package com.nexgo.payment.action.confirmamount;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 金额确认界面
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年8月25日
 */
public class ConfirmAmountDialogFragment extends DialogFragment {
    public static final String TRANS_AMOUNT = "amount";
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.iv_cancel)
    ImageView ivCancel;
    Unbinder unbinder;

    private OnDialogListener mListener;
    private String mAmount;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mAmount = getArguments().getString(TRANS_AMOUNT);
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (mAmount != null) {
            if (!TextUtils.isEmpty(mAmount)) {
                tvAmount.setText(StringUtils.formatAmount(mAmount, SysConstant.FUND_DIGITS));
            }
        }
        btnConfirm.setOnClickListener(view1 -> {
            dismiss();
            if (mListener != null) {
                mListener.onConfirm();
            }
        });
        ivCancel.setOnClickListener(view12 -> dismiss());
    }

    public static ConfirmAmountDialogFragment newInstance(String amount) {
        ConfirmAmountDialogFragment dialog = new ConfirmAmountDialogFragment();
        Bundle b = new Bundle();
        b.putString(TRANS_AMOUNT, amount);
        dialog.setArguments(b);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_confirm_amount, null);
        unbinder = ButterKnife.bind(this, view);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCancelable(false);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void setDialogListener(OnDialogListener listener) {
        mListener = listener;
    }

    public interface OnDialogListener {
        void onConfirm();
    }
}
